import {fileURLToPath, URL} from 'node:url'
import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import {createSvgIconsPlugin} from "vite-plugin-svg-icons";
import {viteMockServe} from "vite-plugin-mock";
import UnoCSS from "unocss/vite";

import path from "path";

const CWD = process.cwd();
const pathSrc = path.resolve(__dirname, 'src')

// https://cn.vite.dev/config/
export default defineConfig(({mode}) => {
    const env = loadEnv(mode, CWD)
    ////// console.log("【环境变量】", env, '\n\n')
    return {
        base: env.VITE_BASE,
        server: {
            port: 6969,
            host: true,
            open: true,
            proxy: {
                '/dev-api': {
                    target: env.VITE_API_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/dev-api/, ''),
                },
                '/prod-api': {
                    target: env.VITE_API_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/prod-api/, ''),
                },
            },
        },
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url)),
                '~': path.resolve(__dirname, './'),
            },
        },
        plugins: [
            vue(),
            vueJsx(),
            vueDevTools(),
            AutoImport({
                imports: [
                    'vue',
                    'vue-router',
                    'pinia'
                ],
                dts: true,
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/asset/svg')],
                symbolId: 'icon-[name]',
            }),
            UnoCSS(),
            //Mock
            viteMockServe({
                enable: env.VITE_MOCK_OPEN === "true", // true/false
                mockPath: 'mock', // 设置mockPath为根目录下的mock目录
                watchFiles: true, // 是否监视文件更改
                logger: true, //是否在控制台显示请求日志
            }),
        ]

    }
})



